package com.sx.config;

import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;

import javax.annotation.PostConstruct;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

@Data
@Configuration
@ConfigurationProperties(prefix = "ykt")
public class YktConfig {

    /**
     * 雨课堂cookie
     */
    private String cookie;

    /**
     * 雨课堂user-agent
     */
    private String userAgent;

    /**
     * 雨课堂xtbz
     */
    private String xtbz;

    @Value("classpath:cookie.txt")
    private Resource cookieFile;

    @PostConstruct
    public void init() throws IOException {
        if (cookie != null){
            return;
        }

        assert cookieFile != null && cookieFile.exists():"cookie.txt文件不存在或内容为空";
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(cookieFile.getInputStream()))) {
            StringBuilder cookieBuilder = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                cookieBuilder.append(line);
            }
            this.cookie = cookieBuilder.toString();
        }
    }
}
